package wellness.activities;

import java.text.SimpleDateFormat;

import wellness.activities.beans.LoadBeans;
import wellness.activities.beans.UsuarioBean;
import wellness.activities.data.UsuarioData;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Profile extends Activity {
	
	Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.profile);
		context = this;

		ImageView goBack = (ImageView) findViewById(R.id.imageViewGoBack);
		goBack.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent();
				setResult(RESULT_OK, intent);
				finish();
			}
		});

		ImageView goFreeTime = (ImageView) findViewById(R.id.ImageViewGoFreeTime);
		goFreeTime.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent myIntent = new Intent(Profile.this, FreeTime.class);
				startActivityForResult(myIntent, 0);
			}
		});

		ImageView goHome = (ImageView) findViewById(R.id.imageViewGoHome);
		goHome.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent intent = new Intent(Profile.this, Main.class);
				startActivity(intent);
			}
		});
		
		ImageView salvar = (ImageView) findViewById(R.id.imageViewSave);
		salvar.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				String pesoStr = ((EditText) findViewById(R.id.editTextPeso)).getText().toString();
				String alturaStr = ((EditText) findViewById(R.id.editTextAltura)).getText().toString();
				
				if(pesoStr.trim().equals("")){
					Toast.makeText(context, "Informe o peso", Toast.LENGTH_SHORT).show();
				}else if(alturaStr.trim().equals("")){
					Toast.makeText(context, "Informe a altura", Toast.LENGTH_SHORT).show();
				}else{
					LoadBeans.getUsuario().setPeso(Double.parseDouble(pesoStr));
					LoadBeans.getUsuario().setAltura(Double.parseDouble(alturaStr));;
					
					UsuarioData data = new UsuarioData(context);
					data.atualizar(Double.parseDouble(pesoStr), Double.parseDouble(alturaStr));
					
					Intent intent = new Intent(Profile.this, Main.class);
					startActivity(intent);	
				}
			}
		});
		
		UsuarioBean u = LoadBeans.getUsuario();
		TextView nome = (TextView) findViewById(R.id.textViewNome);
		nome.setText(u.getNome());
		
		TextView sexo = (TextView) findViewById(R.id.textViewSexo);
		sexo.setText(u.getSexo());
		
		TextView nascimento = (TextView) findViewById(R.id.textViewDtNascimento);
		SimpleDateFormat br = new SimpleDateFormat("dd/MM/yyyy");
		nascimento.setText(br.format(u.getDataNascimento().getTime()));
		
		EditText peso = (EditText) findViewById(R.id.editTextPeso);
		peso.setText(String.valueOf(u.getPeso()));
		
		EditText altura = (EditText) findViewById(R.id.editTextAltura);
		altura.setText(String.valueOf(u.getAltura()));

	}

}
